package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.XStream;

public class ContactDataGenerator extends DataGeneratorBase {
	public static void main(String[] args) throws IOException {
		if (!checkArguments(args)){
			return;
		}
		
		int amount = Integer.parseInt(args[0]);
		File file = new File(args[1]);
		String format = args[2];
		
		List<ContactData> data = generateRandomContacts(amount);
		if ("csv".equals(format)) {
			saveContactsToCsvFile(data, file);
		} else if ("xml".equals(format)) {
			saveContactsToXmlFile(data, file);
		} 
	}

	private static void saveContactsToXmlFile(List<ContactData> data, File file) throws IOException  {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		String xml = xstream.toXML(data);
		FileWriter writer = new FileWriter(file);
		writer.write(xml);
		writer.close();
		
	}

	private static void saveContactsToCsvFile(List<ContactData> data, File file) throws IOException  {
		FileWriter writer = new FileWriter(file);
		for (ContactData contact : data) {
			writer.write(contact.getFName()+","+
					     contact.getLName()+","+
					     contact.getPrmAddress()+","+
					     contact.getPrmPhone() +","+
					     contact.getMobPhone()+","+
					     contact.getWrkPhone()+","+
					     contact.getPrmEmail()+","+
					     contact.getSecEmail()+","+
					     contact.getBrthDay()+","+
					     contact.getSecAddress()+","+
					     contact.getSecPhone()+",!\n");
		}
		writer.close();
	}


	protected static List<ContactData> generateRandomContacts(int amount) {
		List<ContactData> list = new ArrayList<ContactData>();
		for (int i =0; i<amount; i++){
			ContactData contact;	
			contact = new ContactData()
				.withFName(generateRandomString(5,0))
				.withLName(generateRandomString(5,0))
				.withPrmAddress(generateRandomString(20,10))
				.withPrmPhone(generateRandomString(20,10))
				.withMobPhone(generateRandomString(20,10))
				.withWrkPhone(generateRandomString(20,10))
				.withPrmEmail(generateRandomString(20,10))
				.withSecEmail(generateRandomString(20,10))
				.withBrthDay(generateRandomDate())
				.withSecAddress(generateRandomString(20,10))
				.withSecPhone(generateRandomString(20,10));
		list.add(contact);
		}
		
		return list;	
		}


	public static List<ContactData> loadContactsFromXmlFile(File file) throws Exception {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		return (List<ContactData>) xstream.fromXML(file);
	}

	public static List<ContactData> loadContactsFromCsvFile(File file) throws Exception {
		List<ContactData> list = new ArrayList<ContactData>();
		FileReader reader = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(reader);
		
		String line = bufferedReader.readLine();
		while (line != null) {
			String[] part = line.split(",");
			ContactData contact = new ContactData()
				.withFName((part[0].equals("null")) ? null : (part[0]))
				.withLName((part[1].equals("null")) ? null : (part[1]))
				.withPrmAddress((part[2].equals("null")) ? null : (part[2]))
				.withPrmPhone((part[3].equals("null")) ? null : (part[3]))
				.withMobPhone((part[4].equals("null")) ? null : (part[4]))
				.withWrkPhone((part[5].equals("null")) ? null : (part[5]))
				.withPrmEmail((part[6].equals("null")) ? null : (part[6]))
				.withSecEmail((part[7].equals("null")) ? null : (part[7]))
				.withBrthDay((part[8].equals("null")) ? null : (part[8]))
				.withSecAddress((part[9].equals("null")) ? null : (part[9]))
				.withSecPhone((part[10].equals("null")) ? null : (part[10]));
			list.add(contact);
			line = bufferedReader.readLine();
		}
		
		bufferedReader.close();
		return list;
	}
}
